package net.mcreator.compresstools.procedures;

import net.minecraft.world.World;
import net.minecraft.world.IWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.ItemStack;
import net.minecraft.inventory.Inventory;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.Entity;
import net.minecraft.block.Blocks;
import net.minecraft.block.Block;

import net.mcreator.compresstools.item.CompressIronPiaxeItem;
import net.mcreator.compresstools.CompressToolsMod;

import java.util.Map;

public class CompressIronPiaxeBrokedProcedure {

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("world") == null) {
			if (!dependencies.containsKey("world"))
				CompressToolsMod.LOGGER.warn("Failed to load dependency world for procedure CompressIronPiaxeBroked!");
			return;
		}
		if (dependencies.get("x") == null) {
			if (!dependencies.containsKey("x"))
				CompressToolsMod.LOGGER.warn("Failed to load dependency x for procedure CompressIronPiaxeBroked!");
			return;
		}
		if (dependencies.get("y") == null) {
			if (!dependencies.containsKey("y"))
				CompressToolsMod.LOGGER.warn("Failed to load dependency y for procedure CompressIronPiaxeBroked!");
			return;
		}
		if (dependencies.get("z") == null) {
			if (!dependencies.containsKey("z"))
				CompressToolsMod.LOGGER.warn("Failed to load dependency z for procedure CompressIronPiaxeBroked!");
			return;
		}
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				CompressToolsMod.LOGGER.warn("Failed to load dependency entity for procedure CompressIronPiaxeBroked!");
			return;
		}
		IWorld world = (IWorld) dependencies.get("world");
		double x = dependencies.get("x") instanceof Integer ? (int) dependencies.get("x") : (double) dependencies.get("x");
		double y = dependencies.get("y") instanceof Integer ? (int) dependencies.get("y") : (double) dependencies.get("y");
		double z = dependencies.get("z") instanceof Integer ? (int) dependencies.get("z") : (double) dependencies.get("z");
		Entity entity = (Entity) dependencies.get("entity");
		double fx = 0;
		double fy = 0;
		double fz = 0;
		double nx = 0;
		double ny = 0;
		fx = ((x - entity.getPersistentData().getDouble("fx") * 3) - 3);
		fy = ((y - entity.getPersistentData().getDouble("fy") * 3) - 3);
		fz = ((z - entity.getPersistentData().getDouble("fz") * 3) - 3);
		nx = fx;
		ny = fy;
		if (CompressIronPiaxeItem.block.canHarvestBlock((world.getBlockState(new BlockPos((int) x, (int) y, (int) z))))) {
			for (int index0 = 0; index0 < (int) (7); index0++) {
				for (int index1 = 0; index1 < (int) (7); index1++) {
					for (int index2 = 0; index2 < (int) (7); index2++) {
						if ((world.getBlockState(new BlockPos((int) fx, (int) fy, (int) fz))).getBlock() == Blocks.STONE) {
							world.destroyBlock(new BlockPos((int) fx, (int) fy, (int) fz), false);
							if (world instanceof World && !world.isRemote()) {
								ItemEntity entityToSpawn = new ItemEntity((World) world, fx, fy, fz, new ItemStack(Blocks.STONE));
								entityToSpawn.setPickupDelay((int) 10);
								world.addEntity(entityToSpawn);
							}
						} else if (((world instanceof World)
								? ((World) world).getRecipeManager()
										.getRecipe(IRecipeType.SMELTING, new Inventory(
												(new ItemStack((world.getBlockState(new BlockPos((int) fx, (int) fy, (int) fz))).getBlock()))),
												(World) world)
										.isPresent()
								: false)
								&& !BlockTags.getCollection().getTagByID(new ResourceLocation("forge:forge_block"))
										.contains((world.getBlockState(new BlockPos((int) fx, (int) fy, (int) fz))).getBlock())) {
							if (world instanceof World && !world.isRemote()) {
								ItemEntity entityToSpawn = new ItemEntity((World) world, fx, fy, fz,
										((world instanceof World && ((World) world).getRecipeManager().getRecipe(IRecipeType.SMELTING, new Inventory(
												(new ItemStack((world.getBlockState(new BlockPos((int) fx, (int) fy, (int) fz))).getBlock()))),
												((World) world)).isPresent())
														? ((World) world).getRecipeManager().getRecipe(IRecipeType.SMELTING, new Inventory(
																(new ItemStack((world.getBlockState(new BlockPos((int) fx, (int) fy, (int) fz)))
																		.getBlock()))),
																(World) world).get().getRecipeOutput().copy()
														: ItemStack.EMPTY));
								entityToSpawn.setPickupDelay((int) 10);
								world.addEntity(entityToSpawn);
							}
							world.destroyBlock(new BlockPos((int) fx, (int) fy, (int) fz), false);
						} else if (!BlockTags.getCollection().getTagByID(new ResourceLocation("forge:unbreak"))
								.contains((world.getBlockState(new BlockPos((int) fx, (int) fy, (int) fz))).getBlock())) {
							if (world instanceof World) {
								Block.spawnDrops(world.getBlockState(new BlockPos((int) fx, (int) fy, (int) fz)), (World) world,
										new BlockPos((int) fx, (int) fy, (int) fz));
								world.destroyBlock(new BlockPos((int) fx, (int) fy, (int) fz), false);
							}
						}
						fx = (fx + 1);
					}
					fx = nx;
					fy = (fy + 1);
				}
				fy = ny;
				fz = (fz + 1);
			}
		}
		entity.getPersistentData().putBoolean("bkd", (true));
	}
}
